package cn.yunyichina.scheduler.jobs.service.register.impl;

import cn.yunyichina.scheduler.jobs.constant.OrderConstant;
import cn.yunyichina.scheduler.jobs.dao.register.MedicalReserveRegisterTimeoutDao;
import cn.yunyichina.scheduler.jobs.entity.register.RegisterRecord;
import cn.yunyichina.scheduler.jobs.service.register.MedicalReserveRegisterTimeoutService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Lullaby on 2016/8/10.
 */
@Service
public class MedicalReserveRegisterTimeoutServiceImpl implements MedicalReserveRegisterTimeoutService {

    private static final Logger logger = LoggerFactory.getLogger(MedicalReserveRegisterTimeoutServiceImpl.class);

    @Resource
    private MedicalReserveRegisterTimeoutDao medicalReserveRegisterTimeoutDao;

    public void doExecute() {
        List<RegisterRecord> records = medicalReserveRegisterTimeoutDao.getTimeoutMedicalReserveRegisterRecords();
        if (!CollectionUtils.isEmpty(records)) {
            int count = records.size();
            logger.info("医保预约挂号超时未支付订单共[{}]条", count);
            List<Map<String, String>> paramsList = new ArrayList<>();
            for (int i = 0; i < count; i++) {
                RegisterRecord record = records.get(i);
                Map<String, String> map = new HashMap<>();
                map.put("tableName", record.getTableName());
                map.put("id", record.getId());
                paramsList.add(map);
            }
            int result = medicalReserveRegisterTimeoutDao.setMedicalRegisterRecordTimeout(paramsList, OrderConstant.PAYMENT_TIMEOUT_REG_STATUS);
            logger.info("成功更新[{}]条订单状态", result);
        } else {
            logger.info("未查询到医保预约挂号超时未支付订单");
        }
    }

}
